﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using MM.Model;
using MM.DBUtility;

namespace MM.DAL
{
    public class T_MobileInfo
    {
        public bool Exists(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_MobileInfo");
            strSql.Append(" where ID=@ID ");
            SqlParameter[] parameters = {
					new SqlParameter("@ID", SqlDbType.Int)};
            parameters[0].Value = ID;

            return DbHelperSql.Exists(strSql.ToString(), parameters);
        }

        public void Add(MM.Model.T_MobileInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_MobileInfo(");
            strSql.Append("Target,Content,Status)");
            strSql.Append(" values (");
            strSql.Append("@Target,@Content,@Status)");
            SqlParameter[] parameters = {
					new SqlParameter("@Target", SqlDbType.VarChar,50),
					new SqlParameter("@Content", SqlDbType.VarChar,500),
					new SqlParameter("@Status", SqlDbType.Bit)};
            parameters[0].Value = model.Target;
            parameters[1].Value = model.Content;
            parameters[2].Value = model.Status.Equals(MM.Model.T_MobileInfo.StatusEnum.Enabled) ? true : false;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Update(MM.Model.T_MobileInfo model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_MobileInfo set ");
            strSql.Append("Target=@Target,");
            strSql.Append("Content=@Content,");
            strSql.Append("Status=@Status");
            strSql.Append(string.Format(" where ID={0} ", model.ID));

            SqlParameter[] parameters = {
					new SqlParameter("@Target", SqlDbType.VarChar,50),
					new SqlParameter("@Content", SqlDbType.VarChar,500),
					new SqlParameter("@Status", SqlDbType.Bit)};
            parameters[0].Value = model.Target;
            parameters[1].Value = model.Content;
            parameters[2].Value = model.Status.Equals(MM.Model.T_MobileInfo.StatusEnum.Enabled) ? true : false;

            DbHelperSql.ExecuteSql(strSql.ToString(), parameters);
        }

        public void Delete(int ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append(string.Format("delete from T_MobileInfo where ID={0} ", ID));
            DbHelperSql.ExecuteSql(strSql.ToString());
        }

        public List<MM.Model.T_MobileInfo> GetListBySQL(string SQL)
        {
            List<MM.Model.T_MobileInfo> modelList = new List<MM.Model.T_MobileInfo>();
            DataSet ds = DbHelperSql.Query(SQL);
            if (ds != null && ds.Tables.Count > 0)
            {
                int intN = ds.Tables[0].Rows.Count;
                if (intN > 0)
                {
                    MM.Model.T_MobileInfo model;
                    for (int i = 0; i < intN; i++)
                    {
                        model = new MM.Model.T_MobileInfo();
                        model.ID = ds.Tables[0].Rows[i]["ID"].ToString();
                        model.Target = ds.Tables[0].Rows[i]["Target"].ToString();
                        model.Content = ds.Tables[0].Rows[i]["Content"].ToString();
                        if (ds.Tables[0].Rows[i]["Status"].ToString().ToLower().Equals("false"))
                            model.Status = MM.Model.T_MobileInfo.StatusEnum.Disabled;
                        else model.Status = MM.Model.T_MobileInfo.StatusEnum.Enabled;
                        modelList.Add(model);
                    }
                }
            }
            return modelList;
        }
    }
}
